package com.dreamer.view.pyramid;

import com.dreamer.domain.account.AccountsName;
import com.dreamer.domain.pyramid.UserBankCard;
import com.dreamer.service.pyramid.AccountsNameHandler;
import com.dreamer.service.pyramid.UserBankCardHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.message.Message;

import java.util.Optional;

@RestController
@RequestMapping("/userBankCard")
public class UserBankCardController {

    @Autowired
    private UserBankCardHandler userBankCardHandler;

    @RequestMapping(value = "/edit.json", method = RequestMethod.POST)
    public Message edit(@ModelAttribute("parameter") UserBankCard userBankCard) {
        UserBankCard accountsName1 = userBankCardHandler.saveOrupdate(userBankCard);
        if(accountsName1 != null){
            return Message.createSuccessMessage();
        }else{
            return Message.createFailedMessage();
        }
    }

    @ModelAttribute("parameter")
    public UserBankCard preprocess(@RequestParam("id") Optional<Integer> id) {
        UserBankCard userBankCard;
        if (id.isPresent()) {
            userBankCard = userBankCardHandler.findById(id.get());
        }else{
            userBankCard = new UserBankCard();
        }
        return userBankCard;
    }

}
